package com.maolong.deadpro.config;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Configuration
@EnableRabbit
@Data
@Slf4j
@ConfigurationProperties("spring.rabbitmq")
public class RabbitConfig {

    private static final String DEAD_EXCHANGE = "dead_exchange";
    private static final String DEAD_QUEUE = "dead_queue";
    private static final String EXCHANGE_NORMAL_NAME="normal_exchange";
    private static final String QUEUE_NORMAL_NAME="normal_queue";
    private String host;
    private int port;
    private String username;
    private String password;

    @Bean
    public CachingConnectionFactory connectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
        return connectionFactory;
    }

    @Bean
    public RabbitTemplate rabbitTemplate(AckCallBack ackCallBack,ReturnCallBack returnCallBack) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory());
        rabbitTemplate.setConfirmCallback(ackCallBack);
        rabbitTemplate.setReturnsCallback(returnCallBack);
        rabbitTemplate.setMandatory(true);
        return rabbitTemplate;
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue(DEAD_QUEUE);
    }
    @Bean
    public TopicExchange deadLetterExchange() {
        return new TopicExchange(DEAD_EXCHANGE);
    }
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(queue()).to(deadLetterExchange()).with("dead.#");
    }

    /**
     * 正常队列
     * @return
     */
    @Bean
    public Queue normalQueue() {
        Map<String, Object> map=new HashMap();
        /**
         * 绑定正常队列与死信交换机，并指定路由的地址
         */
        map.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        map.put("x-dead-letter-routing-key", "dead.#");
        map.put("x-max-length",3);
        return QueueBuilder.durable(QUEUE_NORMAL_NAME)
                .withArguments(map)
                .build();
    }
    @Bean
    public TopicExchange normalExchange() {
        return new TopicExchange(EXCHANGE_NORMAL_NAME);
    }
    @Bean
    public Binding normalBinding() {
        return BindingBuilder.bind(normalQueue()).to(normalExchange()).with("normal.#");
    }
}
